<template>
  <view class="goods-list">
    <block v-for="(item,i) in goodsList" :key="i">
      <!-- 为 my-goods 组件动态绑定 goods 属性的值 -->
      <my-goods :goods="item" @click="gotoGoodsDetail(item)"></my-goods>
    </block>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求参数
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [], // 列表数据
        total: 0, // 总条数
        isLoading:false,//节流阀
      };
    },
    onLoad(options) {
      // 拿到参数
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      //拿到参数
      this.getGoodsList()
    },
    methods: {
      // 获取商品列表数据的方法
      async getGoodsList(cb) {
        
        this.isLoading = true
        // 发起请求
        const {
          data: res
        } = await
        uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        // 放开
        this.isLoading = false
        cb && cb()
        if (res.meta.status !== 200) return uni.$showMsg()
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
      },
      gotoGoodsDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      }
    },
    // 触底事件
    onReachBottom() {
      if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total) return uni.$showMsg("数据加载完毕!")
      if(this.isLoading) return
      // 页面加一
      this.queryObj.pagenum +=1
      this.getGoodsList()
    },
    // 下拉刷新事件
    onPullDownRefresh() {
      this.goodsList = []
      this.queryObj.pagenum = 1
      this.total=0
      this.getGoodsList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>